package it.enea.xlab.firstcrud.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import it.enea.xlab.firstcrud.crudmanager.CRUDManagerRemote;
import it.enea.xlab.firstcrud.entity.Supplier;
import it.enea.xlab.firstcrud.entity.TranslationType;
import it.enea.xlab.firstcrud.entity.TransmissionType;

import javax.naming.InitialContext;
import javax.naming.NamingException;


import org.junit.Test;

public class ClientTest2 {

	/**
	 * @author X-Lab
	 * @param args
	 */
	@Test
	public void main() {
	
		
		System.out.println("");
		System.out.println("-- ClientTest2 Start --");
		System.out.println("");	
		
		try {
			CRUDManagerRemote myBean = createBeanJpa();
			test(myBean);
		} 
		catch (NamingException e) {	e.printStackTrace(); }
	}

	

	public CRUDManagerRemote createBeanJpa() throws NamingException {

		InitialContext ctx = new InitialContext();
		CRUDManagerRemote myBean = (CRUDManagerRemote) ctx.lookup("first-crud-ear-1.0-SNAPSHOT/CRUDManagerImpl/remote");
		
		return myBean;
	}
	

	
	public void test(CRUDManagerRemote myBean)
	{
		System.out.println("-- test method Start --");

		Supplier mySupplier = new Supplier("xlab", new TransmissionType("http"), new TranslationType("eng"));

		Long id = myBean.createSupplier(mySupplier);

		System.out.println("CREATE - id: " + id);

		Supplier mySupplier2 = myBean.readSupplier(id);

		System.out.println("READ: identification value: " + mySupplier2.getIdentificationCode());
		mySupplier2.setIdentificationCode("Cccc");
		myBean.updateSupplier(mySupplier2);

		Supplier mySupplier3 = myBean.readSupplier(id);

		System.out.println("READ2: identification value: " + mySupplier3.getIdentificationCode());		
		mySupplier = new Supplier("xlab2", new TransmissionType("http"), new TranslationType("eng"));

		id = myBean.createSupplier(mySupplier);

		HashMap<Long, Supplier> register = myBean.getRegister();

		ArrayList<Supplier> list=new ArrayList(register.values());
		Iterator<Supplier> iterator=list.iterator();

		while(iterator.hasNext()) {
			System.out.println(iterator.next().getIdentificationCode());
		}

/*		try {
		Supplier[] register = (Supplier[]) myBean.getSupplierRegister().toArray();
		for (int i=0; i<register.length; i++)
			System.out.println(register[i].getIdentificationCode());
		}
		catch (Exception e) {
			e.printStackTrace();
		}*/


		System.out.println("-- test method End --");
	}
}


